package city.spring.modules.work.controller;

import city.spring.modules.work.entity.WorkPlanUserExtEntity;
import city.spring.modules.work.repository.WorkPlanUserExtRepository;
import city.spring.modules.work.service.WorkPlanUserExtService;
import city.spring.modules.work.vo.WorkPlanUserVO;
import com.baomidou.mybatisplus.extension.api.ApiController;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 排班方案的参与人员管理
 *
 * @author HouKunLin
 * @date 2020/6/7 0007 16:42
 */
@RestController
@RequestMapping("/work_plan/{planId}/user")
public class WorkPlanUserController extends ApiController {
    private final WorkPlanUserExtService planUserExtService;
    private final WorkPlanUserExtRepository planUserExtRepository;

    public WorkPlanUserController(WorkPlanUserExtService planUserExtService, WorkPlanUserExtRepository planUserExtRepository) {
        this.planUserExtService = planUserExtService;
        this.planUserExtRepository = planUserExtRepository;
    }

    @ModelAttribute
    public void getPlanId(@PathVariable String planId) {
        if (planId == null) {
            throw new RuntimeException("排班方案ID必须不为空");
        }
    }

    /**
     * 获取排班方案所有人员列表
     *
     * @param planId 方案ID
     */
    @GetMapping
    public Object list(@PathVariable String planId) {
        return success(planUserExtRepository.getPlanUsers(planId));
    }

    /**
     * 给方案添加多个人员信息
     *
     * @param planId 方案ID
     * @param users  所添加的人员信息列表
     * @return
     */
    @PostMapping
    public Object addAll(@PathVariable String planId, @RequestBody List<WorkPlanUserExtEntity> users) {
        // 查出已有的数据
        List<WorkPlanUserVO> dbUsers = planUserExtRepository.getPlanUsers(planId);
        // 数据库已经存在的班次列表
        Set<String> dbUserIdsSet = dbUsers.stream()
                .map(WorkPlanUserExtEntity::getUserId)
                .collect(Collectors.toSet());
        // 过滤掉已经存在数据库的数据
        List<WorkPlanUserExtEntity> needSaveUsers = users.stream()
                .filter(item -> {
                    item.setWorkPlanId(planId);
                    String userId = item.getUserId();
                    // 过滤出不存在数据库中的数据
                    return userId != null && !dbUserIdsSet.contains(userId);
                }).collect(Collectors.toList());
        if (!needSaveUsers.isEmpty()) {
            planUserExtService.saveBatch(needSaveUsers);
        }

        return success(null);
    }

    /**
     * 获取方案的一个人员信息
     *
     * @param planId 方案ID
     * @param extId  关联ID
     * @return
     */
    @GetMapping("{extId}")
    public Object getInfo(@PathVariable String planId, @PathVariable Long extId) {
        return success(planUserExtRepository.getPlanUser(planId, extId));
    }

    /**
     * 修改一个方案关联的人员信息
     *
     * @param planId    方案ID
     * @param extId     关联ID
     * @param extEntity 接收的数据
     * @return
     */
    @PutMapping("{extId}")
    public Object update(@PathVariable String planId, @PathVariable Long extId, @RequestBody WorkPlanUserExtEntity extEntity) {
        extEntity.setId(extId);
        extEntity.setWorkPlanId(planId);
        planUserExtService.updateWorkPlanUserExt(extEntity);
        return success(null);
    }

    /**
     * 删除一个 <strong>排班方案的关联人员信息</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        planUserExtService.deleteWorkPlanUserExt(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>排班方案的关联人员信息</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        planUserExtService.deleteWorkPlanUserExt(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
